<template>
    <div class="list" ref="warpper">
      <div>
      <div class="area">
        <div class="title border-topbottom" >当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.$store.state.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom" >热门城市</div>
        <div class="button-list">
          <div 
          class="button-wrapper" 
          v-for="item of hotCities" 
          :key="item.id"
          @click="hanleCityClick(item.name)"
          >
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area"
      v-for="(item,key) of cities" 
      :key="key"
      :ref="key"
      >
        <div class="title border-bottom" >{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom"
            v-for="innerItem of item"
            :key="innerItem.id"
            @click="hanleCityClick(innerItem.name)"
          >
          {{innerItem.name}}
          </div>
        </div>
      </div>
      </div>
    </div>
</template>

<script>
import Bscroll from "better-scroll";
export default {
  name: "CityList",
  props: {
    city: String,
    hotCities: Array,
    cities: Object,
    letter: String
  },
  updated() {
    // better-scroll会阻止click事件
    this.scroll = new Bscroll(this.$refs.warpper,{click: true});
  },
  watch: {
    // 监听父组件传来的letter
    letter() {
      // letter不为空进入判断
      if (this.letter) {
        // 拿到当前点击的字母
        const element = this.$refs[this.letter][0];
        // console.log(element);
        // 滚动到当前点击的字母
        this.scroll.scrollToElement(element);
      }
    }
  },
  // Vuex
  methods:{
    hanleCityClick(city){
      this.$store.commit('changeCity',city)
      this.$router.push('/')
    }
  }
};
</script>

<style lang="less" scoped>
@import "~@/assets/styles/varibles.less";
.list {
  position: absolute;
  top: 1.58rem;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  // background-color: red;
  .title {
    line-height: 0.54rem;
    background-color: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
    // border-bottom: 0.02rem solid #777;
  }
  .border-topbottom {
    &:after {
      border-color: #ccc;
    }
    &:before {
      border-color: #ccc;
    }
  }
  .border-bottom {
    &:before {
      border-color: #ccc;
    }
  }
  .button-list {
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;
    overflow: hidden;
    .button-wrapper {
      float: left;
      width: 33.33%;
      .button {
        padding: 0.1rem 0;
        margin: 0.1rem;
        text-align: center;
        border: 0.02rem solid #ccc;
        border-radius: 0.06rem;
      }
    }
  }
  .item {
    line-height: 0.76rem;
    padding-left: 0.2rem;
  }
}
</style>
